<html>
<head>
    <script src="../tma.js"></script>
    <script>
        tma.onload = function () {
            // All libraries are loaded asynchronously.
            var screen = new TmaScreen(320, 320);
            screen.attachTo(TmaScreen.BODY);
            screen.fill("rgba(0, 0, 0, 1)");
            screen.afterimage("rgba(0, 0, 0, 0.1)");
            screen.blur(0.25, 0.2, 0.7, 0, 0, false);

            function Particle () { TmaParticle.apply(this, arguments); }
            Particle.prototype = new TmaParticle(null, 0);
            Particle.prototype.constructor = Particle;

            var container = new TmaParticle.Container(Particle);

            Particle.emit = function () {
                container.add();
            };

            Particle.prototype.initialize = function () {
                this.x = Math.random() * screen.width;
                this.y = Math.random() * screen.height;
                this.vx = 0;
                this.vy = 0;
                this.h = Math.random() * 360;
                this.s = Math.random();
                this.v = Math.random();
            };

            Particle.prototype.update = function () {
                var dx = screen.width / 2 - this.x;
                var dy = screen.height / 2 - this.y;
                if (dx * dx + dy * dy < 1024)
                    return false;
                this.vx += dx / 1024;
                this.vy += dy / 1024;
                this.x += this.vx / 8;
                this.y += this.vy / 8;
                this.h = (this.h + 1) % 360;
                if (this.x < 0 || screen.width <= this.x ||
                        this.y < 0 || screen.height <= this.y)
                    return false;
                screen.setPixel(~~this.x, ~~this.y, this.h, this.s, this.v,
                        0xff, true);
                return true;
            };

            function main () {
                for (var i = 0; i < 16; i++)
                    Particle.emit();
                var data = screen.lock(TmaScreen.LOCK_WITH_SCREEN).data;
                container.update();
                screen.unlock();
            }

            setInterval(main, 20);
        };
    </script>
</head>
</html>
